package org.tio.site.init;

import java.io.IOException;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.tio.core.ssl.SslConfig;
import org.tio.http.common.HttpConfig;
import org.tio.http.common.session.id.impl.SnowflakeSessionIdGenerator;
import org.tio.http.server.HttpServerStarter;
import org.tio.http.server.handler.DefaultHttpRequestHandler;
import org.tio.http.server.mvc.Routes;
import org.tio.server.ServerGroupContext;
import org.tio.site.TioSiteStarter;
import org.tio.site.http.TioSiteHttpServerInterceptor;
import org.tio.site.utils.Threads;
import org.tio.utils.SystemTimer;
import org.tio.utils.time.Time;

import com.jfinal.kit.PropKit;

/**
 * @author tanyaowu
 * 2017年7月19日 下午4:59:04
 */
public class HttpServerInit {
	private static Logger log = LoggerFactory.getLogger(HttpServerInit.class);

	//	public static HttpConfig httpConfig;

	//	public static IHttpRequestHandler requestHandler;

	public static HttpServerStarter httpServerStarter;

	public static String protocol = "http";

	public static void init() throws Exception {
		long start = SystemTimer.currentTimeMillis();

		PropInit.init();

		int port = PropKit.getInt("http.port");//启动端口
		String pageRoot = PropKit.get("http.page");//html/css/js等的根目录，支持classpath:，也支持绝对路径
		String[] scanPackages = new String[] { TioSiteStarter.class.getPackage().getName() };//tio mvc需要扫描的根目录包

		HttpConfig httpConfig = new HttpConfig(port, null, null, null);
		httpConfig.setPageRoot(pageRoot);

		//更高效的Snowflake sessionId生成器，取代默认的sessionId生成器
		SnowflakeSessionIdGenerator sessionIdGenerator = new SnowflakeSessionIdGenerator(1, 1);
		httpConfig.setSessionIdGenerator(sessionIdGenerator);

		Routes routes = new Routes(scanPackages);
		DefaultHttpRequestHandler requestHandler = new DefaultHttpRequestHandler(httpConfig, routes);
		requestHandler.setHttpServerInterceptor(TioSiteHttpServerInterceptor.me);

		httpServerStarter = new HttpServerStarter(httpConfig, requestHandler, Threads.tioExecutor, Threads.groupExecutor);

		ServerGroupContext serverGroupContext = httpServerStarter.getServerGroupContext();
		serverGroupContext.ipStats.addDurations(new Long[] { Time.MINUTE_1, Time.HOUR_1, Time.DAY_1 }, null);
		initSsl(serverGroupContext);

		httpServerStarter.start();

		long end = SystemTimer.currentTimeMillis();
		long iv = end - start;
		log.info("Tio " + protocol + " Server启动完毕,耗时:{}ms,访问地址:" + protocol + "://127.0.0.1:{}", iv, port);
	}

	private static void initSsl(ServerGroupContext serverGroupContext) throws Exception {
		String keyStoreFile = PropKit.get("ssl.keystore", null);
		String trustStoreFile = PropKit.get("ssl.truststore", null);
		String keyStorePwd = PropKit.get("ssl.pwd", null);

		if (StringUtils.isNotBlank(keyStoreFile) && StringUtils.isNotBlank(trustStoreFile)) {
			SslConfig sslConfig = SslConfig.forServer(keyStoreFile, trustStoreFile, keyStorePwd);
			serverGroupContext.setSslConfig(sslConfig);

			protocol = "https";
		}
	}

	/**
	 * @param args
	 * @author tanyaowu
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
	}

	/**
	 *
	 * @author tanyaowu
	 */
	public HttpServerInit() {
	}
}
